package com.tanhua.dubbo.api;


import com.tanhua.model.db.Comment;
import com.tanhua.model.db.Movement;
import com.tanhua.model.db.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public List<Comment> comment(Integer page, Integer pageSize, String movementId) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(2);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.by("created"))).skip((page - 1) * pageSize).limit(pageSize);
        return mongoTemplate.find(query, Comment.class);
    }

    @Override
    public List<Comment> commentByVideo(Integer page, Integer pageSize, String videoId) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(videoId)).and("commentType").is(2);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.by("created"))).skip((page - 1) * pageSize).limit(pageSize);
        return mongoTemplate.find(query, Comment.class);
    }

    @Override
    public Integer save(Comment comment) {
        ObjectId publishId = comment.getPublishId();
        Movement byId = mongoTemplate.findById(publishId, Movement.class);
        System.out.println(byId);
        comment.setPublishUserId(byId.getUserId());
        mongoTemplate.save(comment);
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query=Query.query(criteria);
        Update update = new Update();
        if(comment.getCommentType() == 1) { //点赞
            update.inc("likeCount",1);
        }else if(comment.getCommentType() == 2){//评论
            update.inc("commentCount",1);
        }else { //喜欢
            update.inc("loveCount",1);
        }
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        Movement andModify =mongoTemplate.findAndModify(query,update,options,Movement.class);
        if(comment.getCommentType() == 1) { //点赞
            return andModify.getLikeCount();
        }else if(comment.getCommentType() == 2){//评论
            return andModify.getCommentCount();
        }else { //喜欢
            return andModify.getLoveCount();
        }
    }

    @Override
    public boolean hasComment(String id, long userId, int i) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(id)).and("userId").is(userId).and("commentType").is(i);
        return mongoTemplate.exists(Query.query(criteria),Comment.class);
    }

    @Override
    public Integer delete(String id, long userId, int i) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(id)).and("userId").is(userId).and("commentType").is(i);
        mongoTemplate.remove(Query.query(criteria), Comment.class);
//更新并获取最新的动态数据（点赞：点赞总数-1。评论：评论总数-1）
        Query query = Query.query(Criteria.where("id").is(id));
        Update update = new Update();
        if(i == 1) { //点赞
            update.inc("likeCount",-1); //对此字段数据 +1
        }else if(i == 2){ //评论
            update.inc("commentCount",-1); //对此字段数据 +1
        }else { //喜欢
            update.inc("loveCount",-1); //对此字段数据 +1
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //先更新，查询返回最新的结果
        Movement andModify = mongoTemplate.findAndModify(query, update,
                options, Movement.class);
        //构造返回
        if(i == 1) { //点赞
            return andModify.getLikeCount();
        }else if(i == 2){ //评论
            return andModify.getCommentCount();
        }else { //喜欢
            return andModify.getLoveCount();
        }
    }

    @Override
    public Integer saveComments(Comment comment) {
        ObjectId publishId = comment.getPublishId();
        Video byId = mongoTemplate.findById(publishId, Video.class);
        comment.setPublishUserId(byId.getUserId());
        mongoTemplate.save(comment);
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query=Query.query(criteria);
        Update update = new Update();
        if(comment.getCommentType() == 1) { //点赞
            update.inc("likeCount",1);
        }else if(comment.getCommentType() == 2){//评论
            update.inc("commentCount",1);
        }else { //喜欢
            update.inc("loveCount",1);
        }
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        Video andModify =mongoTemplate.findAndModify(query,update,options,Video.class);
        if(comment.getCommentType() == 1) { //点赞
            return andModify.getLikeCount();
        }else if(comment.getCommentType() == 2){//评论
            return andModify.getCommentCount();
        }else { //喜欢
            return andModify.getLoveCount();
        }

    }

    @Override
    public Integer saveCommentLike(Comment comment1) {
        ObjectId publishId = comment1.getPublishId();
        Comment byId = mongoTemplate.findById(publishId, Comment.class);
        System.out.println(byId);
        comment1.setPublishUserId(byId.getUserId());
        mongoTemplate.save(comment1);
        Criteria criteria = Criteria.where("id").is(publishId);
        Query query=Query.query(criteria);
        Update update = new Update();
        if(comment1.getCommentType() == 1) { //点赞
            update.inc("likeCount",1);
        }
        FindAndModifyOptions options=new FindAndModifyOptions();
        options.returnNew(true);
        Comment andModify =mongoTemplate.findAndModify(query,update,options,Comment.class);
        if(comment1.getCommentType() == 1) { //点赞
            return andModify.getLikeCount();
        }
        return null;
    }

    @Override
    public Integer deleteCommentLike(String id, long userId, int i) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(id)).and("userId").is(userId).and("commentType").is(i);
        mongoTemplate.remove(Query.query(criteria), Comment.class);
        //更新并获取最新的动态数据（点赞：点赞总数-1。评论：评论总数-1）
        Query query = Query.query(Criteria.where("id").is(id));
        Update update = new Update();
        if(i == 1) { //点赞
            update.inc("likeCount",-1); //对此字段数据 +1
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //先更新，查询返回最新的结果
        Comment andModify = mongoTemplate.findAndModify(query, update,
                options, Comment.class);
        //构造返回
        if(i == 1) { //点赞
            return andModify.getLikeCount();
        }
        return null;

    }

}